package com.pearadmin.pro.modules.school.service.impl;

import java.util.List;
import java.util.ArrayList;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.tools.core.GeneralConvertUtils;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;

import com.pearadmin.pro.modules.school.domain.*;
import com.pearadmin.pro.modules.school.dto.CourseBasicInformationDTO;
import com.pearadmin.pro.modules.school.dto.SchoolDTO;
import com.pearadmin.pro.modules.school.param.CourseBasicInformationRequest;
import com.pearadmin.pro.modules.school.repository.CourseBasicInformationRepository;
import com.pearadmin.pro.modules.school.service.CbiSorService;
import com.pearadmin.pro.modules.school.service.CourseBasicInformationService;
import com.pearadmin.pro.modules.sys.domain.BusinessFile;
import com.pearadmin.pro.modules.sys.service.BusinessFileService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 课程基本信息Service业务层处理
 *
 * @author ChenJian
 * @date 2021-08-29
 */
@Service
public class CourseBasicInformationServiceImpl extends ServiceImpl<CourseBasicInformationRepository, CourseBasicInformation> implements CourseBasicInformationService
{
    @Resource
    private CourseBasicInformationRepository courseBasicInformationRepository;
    @Resource
    private BusinessFileService businessFileService;
    @Resource
    private CbiSorService cbiSorService;

    /**
     * 查询课程基本信息
     * @param request
     * @return 课程基本信息 分页集合
     * */
    @Override
    public PageResponse<CourseBasicInformation> page(CourseBasicInformationRequest request)
    {
        LambdaQueryWrapper<CourseBasicInformation> query = new LambdaQueryWrapper<CourseBasicInformation>();
        query.eq(ObjectUtil.isNotEmpty(request.getCourseCode()),CourseBasicInformation::getCourseCode,request.getCourseCode());
        return Pageable.of(request,(()->  courseBasicInformationRepository.selectList(query)));
    }

    @Override
    public Boolean saveOrUpdate(CourseBasicInformationDTO courseBasicInformation) {
        CourseBasicInformation courseBasicInformationdb= GeneralConvertUtils.conv(courseBasicInformation, CourseBasicInformation.class);
        saveOrUpdate(courseBasicInformationdb);
        if (!ObjectUtils.isEmpty(courseBasicInformation.getCbiSorList())) {
            //保存附件
            List<CbiSor> cbiSorList = GeneralConvertUtils.convert2List(courseBasicInformation.getCbiSorList(), CbiSor.class);
            cbiSorList.stream().forEach(s -> {
                s.setCourseBasicInfoId(courseBasicInformationdb.getId());
            });
            cbiSorService.saveOrUpdateBatch(cbiSorList);
        }
        if (!ObjectUtils.isEmpty(courseBasicInformation.getFiles())) {
            //保存附件
            List<BusinessFile> businessFileList = GeneralConvertUtils.convert2List(courseBasicInformation.getFiles(), BusinessFile.class);
            businessFileList.stream().forEach(s -> {
                s.setBusinessId(courseBasicInformation.getId());
            });
            businessFileService.saveOrUpdateBatch(businessFileList);
        }
        return true;
    }

    /**
     * 查询课程基本信息
     *
     * @param id 课程基本信息ID
     * @return 课程基本信息
     */
    @Override
    public CourseBasicInformationDTO selectCourseBasicInformationById(Long id)
    {
        CourseBasicInformationDTO request = GeneralConvertUtils.conv(courseBasicInformationRepository.selectById(id), CourseBasicInformationDTO.class);

        LambdaQueryWrapper<CbiSor> query = new LambdaQueryWrapper<>();
        query.eq(CbiSor::getCourseBasicInfoId,request.getId());
        request.setCbiSorList(cbiSorService.list(query));

        LambdaQueryWrapper<BusinessFile> queryFile = new LambdaQueryWrapper<BusinessFile>();
        queryFile.eq(BusinessFile::getBusinessId,request.getId());
        request.setFiles(businessFileService.list(queryFile));

        return request;
    }

}
